package com.chenyi.leetcode.editor.cn;

import java.util.Arrays;

/**
 * [88]合并两个有序数组
 * @author by chenyi
 * @className MergeSortedArray
 * @date 2022-06-28 17:30:01
 */
public class MergeSortedArray{
    public static void main(String[] args) {
        Solution solution = new MergeSortedArray().new Solution();
        int[] nums1 = {2, 0};
        int[] nums2 = {1};
        solution.merge(nums1, 1, nums2, nums2.length);
        System.out.println(Arrays.toString(nums1));
    }
    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = 0, p2 = 0;
        int[] sorted = new int[m + n];
        int cur;
        while (p1 < m || p2 < n) {
            if (p1 == m) {
                cur = nums2[p2++];
            } else if (p2 == n) {
                cur = nums1[p1++];
            } else if (nums1[p1] < nums2[p2]) {
                cur = nums1[p1++];
            } else {
                cur = nums2[p2++];
            }
            sorted[p1 + p2 - 1] = cur;
        }
        System.arraycopy(sorted, 0, nums1, 0, sorted.length);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
